package com.gxa.jjty.phy.service.impl;

import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.ServerAndTorderVo;
import com.gxa.jjty.core.pojo.Torder;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import com.gxa.jjty.phy.mapper.ServerAndTorderVoMapper;
import com.gxa.jjty.phy.service.ServerAndToderVoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service("serverAndToderVoService")
public class ServerAndTorderVoServiceImpl extends BaseServiceImpl<ServerAndTorderVo>  implements ServerAndToderVoService {

    @Autowired
    private ServerAndTorderVoMapper serverAndTorderVoMapper;

    @Override
    public List<ServerAndTorderVo> selectByNameOrServer(String search, Integer orderState, Long phyId) throws BusinessException {
        List<ServerAndTorderVo> serverAndTorderVos = null;
        if(orderState==1){
            Example example  = new Example(Torder.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("orderNumber",search);
            criteria.andLike("orderState","等待系统分配");
            criteria.orLike("orderUser","%"+search+"%");
            criteria.andLike("orderState","等待系统分配");
            criteria.orLike("serveType","%"+search+"%");
            criteria.andLike("orderState","等待系统分配");
            example.orderBy("orderId");
            Example example1 = new Example(Torder.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.orLike("orderNumber",search);
            criteria1.andLike("transferPhyId", "%/"+String.valueOf(phyId)+"/%");
            criteria1.orLike("orderUser","%"+search+"%");
            criteria1.andLike("transferPhyId", "%/"+String.valueOf(phyId)+"/%");
            criteria1.orLike("serveType","%"+search+"%");
            criteria1.andLike("transferPhyId", "%/"+String.valueOf(phyId)+"/%");
            serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
            List<ServerAndTorderVo> serverAndTorderVos1 = serverAndTorderVoMapper.selectByExample(example1);
            for(int i=0;i<serverAndTorderVos1.size();i++){
                serverAndTorderVos.add(serverAndTorderVos1.get(i));
            }

        }else if(orderState == 2){
            Example example = new Example(Torder.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("orderUser","%"+search+"%");
            criteria.andLike("orderState","陪护员已接单");
            criteria.andLike("phyId", String.valueOf(phyId));
            criteria.orLike("serveType","%"+search+"%");
            criteria.andLike("orderState","陪护员已接单");
            criteria.andLike("phyId", String.valueOf(phyId));
            example.orderBy("orderId");
            serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        }else if(orderState==3){
            Example example = new Example(Torder.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("orderUser","%"+search+"%");
            criteria.andLike("orderState","服务中");
            criteria.andLike("phyId", String.valueOf(phyId));
            criteria.orLike("serveType","%"+search+"%");
            criteria.andLike("orderState","服务中");
            criteria.andLike("phyId", String.valueOf(phyId));
            example.orderBy("orderId");
            serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        }else if(orderState==4){
            Example example = new Example(Torder.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("orderUser","%"+search+"%");
            criteria.andLike("orderState","已完成");
            criteria.andLike("phyId", String.valueOf(phyId));
            criteria.orLike("serveType","%"+search+"%");
            criteria.andLike("orderState","已完成");
            criteria.andLike("phyId", String.valueOf(phyId));
            example.orderBy("flag");
            serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        }else if(orderState==5){
            Example example = new Example(Torder.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("orderUser","%"+search+"%");
            criteria.andLike("orderState","已取消");
            criteria.andLike("phyId", String.valueOf(phyId));
            criteria.orLike("serveType","%"+search+"%");
            criteria.andLike("orderState","已取消");
            criteria.andLike("phyId", String.valueOf(phyId));
            example.orderBy("orderId");
            serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        }else {
            throw new BusinessException(ExceptionCode.NO_VIPCARD);
        }
        return  serverAndTorderVos;
    }

    @Override
    public List<ServerAndTorderVo> select2(ServerAndTorderVo serverAndTorderVo) {
        Example example =  new Example(ServerAndTorderVo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderState","已完成");
        criteria.andEqualTo("phyId",serverAndTorderVo.getPhyId());
        example.orderBy("flag");
        List<ServerAndTorderVo> serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        return serverAndTorderVos;
    }

}
